﻿using mozhi.smarterp.Finance.Files.Repository;
using mozhi.smarterp.Finance.Files.Services;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace mozhi.smarterp.Finance.Files
{
    public class AttachmentAppService : FinanceAppService, IAttachmentAppService
    {
        private AttachmentManager _attachmentManager;

        private IAttachmentRepository _attachmentRepository;

        public AttachmentAppService(AttachmentManager attachmentManager , IAttachmentRepository attachmentRepository)
        {
            _attachmentManager = attachmentManager;
            _attachmentRepository = attachmentRepository;
        }

        public async Task<PagedResultDto<AttachmentDto>> FindAsync(AttachmentPageQuery query)
        {
            var attachments = await _attachmentRepository.FindAsync(query);

            var total = await _attachmentRepository.GetCountAsync(query);

            var result = new PagedResultDto<AttachmentDto>();

            if (attachments.Count == 0)
                return result;

            result.Items = ObjectMapper.Map<List<Attachment>, List<AttachmentDto>>(attachments);

            result.TotalCount = total;

            return result;
        }

        public Task<AttachmentDto> GetAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public async Task<AttachmentDto> CreateAsync(AttachmentCreateDto dto)
        {
            var attachment = await _attachmentManager.CreateAsync(dto.ModuleCode, dto.OwnerId , dto.RemoteFileId);

            return ObjectMapper.Map<Attachment, AttachmentDto>(attachment);
        }

        public async Task DeleteAsync(Guid id)
        {
            await _attachmentManager.DeleteAsync(id);
        }

        
    }
}
